package com.mdd.admin.controller;

import com.mdd.admin.aop.Log;
import com.mdd.admin.service.ICustomerService;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.CustomerCreateValidate;
import com.mdd.admin.validate.CustomerUpdateValidate;
import com.mdd.admin.validate.CustomerSearchValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.CustomerListedVo;
import com.mdd.admin.vo.CustomerDetailVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("api/customer")
@Api(tags = "客户管理")
public class CustomerController {

    @Resource
    ICustomerService iCustomerService;

    @GetMapping("/list")
    @ApiOperation(value="客户列表")
    public AjaxResult<PageResult<CustomerListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated CustomerSearchValidate searchValidate) {
        PageResult<CustomerListedVo> list = iCustomerService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="客户详情")
    public AjaxResult<CustomerDetailVo> detail(@Validated @IDMust() @RequestParam("customerNumber") Integer id) {
        CustomerDetailVo detail = iCustomerService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "客户新增")
    @PostMapping("/add")
    @ApiOperation(value="客户新增")
    public AjaxResult<Object> add(@Validated @RequestBody CustomerCreateValidate createValidate) {
        iCustomerService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "客户编辑")
    @PostMapping("/edit")
    @ApiOperation(value="客户编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody CustomerUpdateValidate updateValidate) {
        iCustomerService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "客户删除")
    @PostMapping("/del")
    @ApiOperation(value="客户删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iCustomerService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
